package com.yanqu.road.dao.impl.activity.suitang.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangData;
import com.yanqu.road.entity.activity.suitang.entity.group.CrossSuiTangGroup;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangWarType;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangStage;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class CrossSuiTangDataDaoImpl extends TempDataObjectDao<CrossSuiTangData> {
    public static String getCrossSuiTangDataSql = "select * from `t_c_suitang_data` where `activityId`=? and `groupId`=?";
    public CrossSuiTangData getCrossSuiTangData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossSuiTangDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCrossSuiTangDataSql = "update `t_c_suitang_data` set battleRound=?,step=?,lockUnion=?,refreshTime=?,process2WinUserId=?,actGroupMap=? where activityId=? and groupId=?";
    @Override
    protected boolean updateData(CrossSuiTangData crossSuiTangData) {
        crossSuiTangData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangData.getBattleRound())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangData.getStep())));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossSuiTangData.getLockUnion()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangData.getProcess2WinUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangData.getActGroupMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangData.getGroupId()));
        if (getDb().executeNoneQuery(updateCrossSuiTangDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSuiTangDataSql = "insert into `t_c_suitang_data`(activityId,groupId,battleRound,step,lockUnion,refreshTime,process2WinUserId,actGroupMap) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossSuiTangData crossSuiTangData) {
        crossSuiTangData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSuiTangData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangData.getBattleRound())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangData.getStep())));
        para.put(i++, new DbParameter(Types.BOOLEAN, crossSuiTangData.getLockUnion()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossSuiTangData.getProcess2WinUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSuiTangData.getActGroupMap())));
        if (getDb().executeNoneQuery(insertCrossSuiTangDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSuiTangData parseData(ResultSet rs) throws SQLException {
        CrossSuiTangData crossSuiTangData = new CrossSuiTangData();
        crossSuiTangData.setActivityId(rs.getInt("activityId"));
        crossSuiTangData.setGroupId(rs.getInt("groupId"));
        crossSuiTangData.setBattleRound(JSON.parseObject(rs.getString("battleRound"), AtomicInteger.class));
        crossSuiTangData.setStep(JSON.parseObject(rs.getString("step"), eSuiTangStage.class));
        crossSuiTangData.setLockUnion(rs.getBoolean("lockUnion"));
        crossSuiTangData.setRefreshTime(rs.getLong("refreshTime"));
        crossSuiTangData.setProcess2WinUserId(rs.getLong("process2WinUserId"));
        crossSuiTangData.setActGroupMap(JSON.parseObject(rs.getString("actGroupMap"), new TypeReference<ConcurrentHashMap<Integer, Map<eSuiTangWarType, Map<Integer, CrossSuiTangGroup>>>>() {}));

        return crossSuiTangData;
    }
}
